*******************************************************************
*     				PATIENT-LEVEL ANALYSIS CODE   	        	  *
* Author: Maggie Shi 	 									      *
* 														          *
* Purpose: This code builds the dataset and produces results from *
* the patient-level analysis in Shi (2023). 					  *
* 														          *
* Last edited: 9/13/2023 									      *
* 														          *
* Notes: Running this code requires access to the HCUP SID/SEDD   *
* 	datasets for Florida, 2010-2016. 						  	  *
* 	To run, all of the code from "~/Code/HCUP-server-code" in the *
* 	in the replication packet should be copied into the           *
*	"${SIDCodePath}" path below. 								  *
* 	The data from "~Local-data/HCUP-server-data" in the 		  *
* 	replication packet should be copied into the 			  	  *
* 	"${SIDDataPath}" path below. 							      *
* 	See https://hcup-us.ahrq.gov/databases.jsp for more details   *
* 	on the acquiring the HCUP SID/SEDD datasets. 				  *
*******************************************************************

cap log close
*--------------------------------------------------
* Set up paths
*--------------------------------------------------
global SIDCodePath 		"/disk/homedirs/nber/m.shi/sid.work/HCUP-server-code" 			// the do-files should be in this folder
global SEDDPath 		"/homes/data/hcup/sedd"											// raw SEDD files
global SIDPath 		    "/homes/data/hcup/sid"											// raw SID files
global SIDOutputPath    "/disk/homedirs/nber/m.shi/sid.work/Output/" 		         	
	cap mkdir "${SIDOutputPath}"
	cap mkdir "${SIDOutputPath}/reg_trad_patientFEs/"
	cap mkdir "${SIDOutputPath}/reg_trad_nopatientFEs/"
	cap mkdir "${SIDOutputPath}/SummaryStats/"
	cap mkdir "${SIDOutputPath}/reg_trad_predictedrisk_patientFEs/"
	cap mkdir "${SIDOutputPath}/reg_trad_predictedrisk_patientFEs/Coefficients"
	cap mkdir "${SIDOutputPath}/reg_trad_predictedrisk_summarystats/"
	

global SIDDataPath      "/disk/homedirs/nber/m.shi/bulk/sid.work/Data/"
	cap mkdir "${SIDDataPath}"
	cap mkdir "${SIDDataPath}/riskpredict"




*--------------------------------------------------
* Load required packages
*--------------------------------------------------
cap ssc install gtools
cap ssc install rangestat
cap ssc install texdoc 
cap ssc install estout, replace
cap ssc install estwrite
ssc install regsave

*--------------------------------------------------
* Build data
*--------------------------------------------------
	// build 2010-2016 analysis dataset from raw HCUP discharge data
	do "${SIDCodePath}/repl_sidsedd_merge.do"

	// build risk prediction dataset
	do "${SIDCodePath}/repl_sidsedd_dataforriskpredict.do" 

	// run risk prediction and save predictions
	do "${SIDCodePath}/repl_sidsedd_riskpredict.do"

	// create variables for regressions
	do "${SIDCodePath}/repl_sidsedd_dataprep.do"	


*--------------------------------------------------
* Summary Statistics
*--------------------------------------------------
	// produce summary statistics for HCUP
	do "${SIDCodePath}/repl_sidsedd_summarystats.do"

*--------------------------------------------------
* Analysis
*--------------------------------------------------
	// main regression specification
	do "${SIDCodePath}/repl_sidsedd_regressions_trad.do"

	// regression interacted with predicted risk decile, then plotting the coefficients
	do "${SIDCodePath}/repl_sidsedd_regressions_predrisk.do"
	do "${SIDCodePath}/repl_sidsedd_regressions_predrisk_plotcoeffs.do"

	// main specification, aggregated to fiscal year
	do "${SIDCodePath}/repl_sidsedd_regressions_trad_fyear.do"

	// heterogeneity by hospital characteristics
	do "${SIDCodePath}/repl_sidsedd_regressions_trad_hetero.do"
	do "${SIDCodePath}/repl_sidsedd_regressions_trad_hetero_plot.do"

	// check for evidence of manipulation of ED arrival time in histograms of ED share
	do "${SIDCodePath}/repl_sidsedd_check_ED_manip.do"




